<?php
/*
 * Created on 25 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
  
  
 class CategorieDAO extends DAO{
 	private $Bean;
 	private function buildFromRow($RowObj){
 				$retBean = new Categorie($RowObj->id);
				$retBean->setVignette($RowObj->vignette);
				$retBean->setOrdre($RowObj->ordre);
				if ($RowObj->parent_id==0){
					$retBean->setParent(null);
				}else{
					$retBean->setParent($this->getById($RowObj->parent_id));	
				}
				$retBean->setNom($this->factory->getNomCategorieDAO()->getById($RowObj->nom_id));
				$retBean->setTooltip($this->factory->getTooltipDAO()->getById($RowObj->tooltip));
				$retBean->setCategories($this->getChildren($RowObj->id));
				return $retBean;
 	}
 	function getById($id){
 		try
 		{
 			$query ="select categorie.id as cat_id,categorie.vignette as vignette,categorie.ordre as ordre,categorie.parent as parent_id,";
 			$query.="cat_fk_nom.nom_categorie as nom_id,categorie_fk_tooltip.tooltip as tooltip";
			$query.=" from categorie,cat_fk_nom,categorie_fk_tooltip"; 
			$query.=" where cat_fk_nom.categorie = categorie.id and categorie.id=$id";
     		$query.=" and categorie_fk_tooltip.categorie=categorie.id and cat_fk_nom.langue=1 and categorie_fk_tooltip.langue=1;";
     		
     		$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			foreach($array_obj as $Bean){
				
				$this->Bean = $this->buildFromRow($Bean);
			}
 		}
 		catch(Exception $e)	{$this->pdo->message;}
 		
 		return $this->Bean;
 	}
 	function getChildren($id){
 		try
 		{
 			$BeanArray= array();
 			$query ="select categorie.id as cat_id,categorie.vignette as vignette,categorie.ordre as ordre,categorie.parent as parent_id,";
 			$query.="cat_fk_nom.nom_categorie as nom_id,categorie_fk_tooltip.tooltip as tooltip";
			$query.=" from categorie,cat_fk_nom,categorie_fk_tooltip"; 
			$query.=" where cat_fk_nom.categorie = categorie.id and categorie.parent=$id";
     		$query.=" and categorie_fk_tooltip.categorie=categorie.id and cat_fk_nom.langue=1 and categorie_fk_tooltip.langue=1;";
     		$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			foreach($array_obj as $Bean){
				array_push($BeanArray, $this->buildFromRow($Bean));
			}
 		}
 		catch(Exception $e)	{$this->pdo->message;}
 		
 		return $BeanArray;
 	}
 	
 	
 }
?>
